from PyQt4.QtGui import *
from PyQt4.QtCore import *

from Tileset import *
from gui.FileSelector import *


class TabTerrain(QWidget):
	def __init__(self):
		QWidget.__init__(self)
		main_layout = QVBoxLayout()
		self.setLayout(main_layout)

		self.tileset = Tileset()
		main_layout.addWidget(self.tileset)


		form = QFormLayout()
		main_layout.addLayout(form)

		self.selector_tileset = FileSelector()
		self.connect(self.selector_tileset, SIGNAL("fileSelected()"), self.on_tileset_selected)
		form.addRow("Tileset:", self.selector_tileset)

		self.lab_tile_selection = QLabel(self)
		self.connect(self.tileset.scene, SIGNAL("tile_selected"), self.set_current_tile)
		form.addRow("Active tile:", self.lab_tile_selection)

	def on_tileset_selected(self):
		filename = self.selector_tileset.getFilename()
		self.load_tileset(filename)

	def get_tileset(self):
		return self.tileset

	def set_current_tile(self, tile_id):
		image = self.tileset.get_tile_by_id(tile_id)
		self.lab_tile_selection.setPixmap(QPixmap.fromImage(image))

	##
	# @param filename: tileset document filename
	#
	def load_tileset(self, filename):

		self.tileset.load_from_xml(filename)
		self.selector_tileset.setFilename(filename)
		# clear active tile
		self.lab_tile_selection.setPixmap(QPixmap())
